package com.LeetCode.Array;

import org.junit.Test;

/**
 * 二维数组搜索
 */
public class FindNumInMatrix {

    public boolean searchMatrix(int[][] matrix, int target) {
        if(matrix.length == 0 || matrix[0].length == 0) return false;
        int n = matrix.length;
        int m = matrix[0].length;
        int i = n-1, j = 0;
        while(i>=0 && j < m){
            if(matrix[i][j] == target)
                return true;
            else if(matrix[i][j] < target){
                j++;
            }else{
                i--;
            }
        }
        return false;
    }

    @Test
    public void test(){
        int[][] matrix= {
                {1,4,7,11,15},
                {2,5,8,12,19},
                {3,6,9,16,22},
                {10,13,14,17,24},
                {18,21,23,26,30}
        };
        System.out.println(searchMatrix(matrix,5));
        System.out.println(searchMatrix(matrix,20));
    }
}
